package com.gm.commonlib.base.baseAdapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public abstract class BaseCommAdapter<T> extends BaseAdapter {
    /**
     * 需要绑定到view的数据集合
     */
    private List<T> mList;

    /**
     * 上下文对象
     */
    public Context mContext;

    /**
     * 构造方法，初始化Context
     *
     * @param context 上下文
     */
    public BaseCommAdapter(Context context, List<T> mList) {
        this.mContext = context;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        } else {
            return 0;
        }
    }

    @Override
    public T getItem(int position) {
        return mList == null ? null : mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 设置需要绑定view的数据集合
     *
     * @param list 数据集合
     */
    public void setList(List<T> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public List<T> getList() {
        return mList;
    }

    /**
     * 设置需要绑定view的数据集合
     *
     * @param data 数据数组
     */
    public void setList(T[] data) {
        ArrayList<T> arrayList = new ArrayList<>(data.length);
        Collections.addAll(arrayList, data);
        setList(arrayList);
    }

    public void clear() {
        if (mList != null && mList.size() > 0) {
            mList.clear();
            setList(mList);
        }
    }

    public abstract View getView(T data, View convertView, int postion);

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getView(mList.get(position), convertView, position);
    }

}
